/*
 * WorkUnit.cpp
 *
 *  Created on: Apr 11, 2009
 *      Author: rpledge
 */

#include "WorkUnit.h"

namespace ComputingObjects {

WorkUnit::WorkUnit(std::string id, WorkUnitData& params)
: myState(idle), myID(id)
{

}

WorkUnit::~WorkUnit() 
{

}

void WorkUnit::attach(SharedWorkUnitObserver anObserver)
{
	myObservers.push_back(anObserver);
}

void WorkUnit::detach(SharedWorkUnitObserver anObserver)
{
	myObservers.remove(anObserver);
}

void WorkUnit::setState(enum state newState)
{
	if (myState != newState) {
		enum state oldState = myState;
		myState = newState;
		std::list<SharedWorkUnitObserver>::iterator anIT(myObservers.begin());
		while (anIT != myObservers.end()) {
			(*anIT)->stateChange(*this, oldState);
			++anIT;
		}
	}
}

static char* stateStrings[] = {
		"idle",
		"pending",
		"dispatched",
		"executing",
		"completed",
		"retrievable"
};

const char* WorkUnit::stateString(enum state aState)
{
	return stateStrings[aState];
}
	
}
